"""
    create by IAmFiveHigh on 2023/9/4
"""


class LoopQueue:
    def __init__(self):
        self._values = []
        self._front = 0
        self._tail = 0

    def is_empty(self):
        return self._front == self._tail

    def enqueue(self, element):
        self._values.append(element)
        self._tail += 1

    def dequeue(self):
        if self.is_empty():
            raise Exception("队列已空")

        element = self._values[self._front]
        self._front += 1
        self._exam_legacy()
        return element

    # 重制数组去掉开头空位
    def _execute_legacy(self):
        size = self._tail - self._front
        for i in range(0, size):
            self._values[i] = self._values[i + self._front]
        self._front = 0
        self._tail = size

    # 检查开头多余的空位
    def _exam_legacy(self):
        size = self._tail - self._front
        if size > 10 and self._front / size > 0.3:
            self._execute_legacy()

    def __str__(self):
        description = "Queue: ["
        for i in range(self._front, self._tail):
            description += f"{str(self._values[i])}, "

        description += "]"
        return description

    # 显示内部values实际存储结果
    def detail(self):
        description = "inner: ["
        for i in self._values:
            description += f"{str(i)}, "
        print(f"{description}]")
